﻿using SRB.Tg.DrawingStandard;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class CushionStoneSteel 
    {

        //PlinthParameter plinthParameter;
        public double Length, Width, Height;
        double out_dia, in_dia;

        List<double> ArrAlongW, ArrAlongL;
        Dictionary<int, int> dicNo = new Dictionary<int, int>();
        int netNumber = 1;
        CushionStoneParameter para;
        double CoverThk = 50;
        string strDiameter = "D";
        //string strMainDiameter = "D";
        List<Tuple<int, eSampleType, eDB, int, double, string>> rebarLst = new List<Tuple<int, eSampleType, eDB, int, double, string>>();
        string bridgeId = "";
        public CushionStoneSteel(string _bridgeId, BridgeStructureType type, out System.Data.DataTable dtProjectTb)
        {
            bridgeId = _bridgeId;
            #region  参数数据初始化
            System.Data.DataTable dt = new System.Data.DataTable();//图纸目录信息表
            //传入bridgeId，查询出mainId
            string sqlMainId = "select * from busi_bridge where id='" + bridgeId + "' ";
            string mainId = MySqlHelper.ExecuteDataTable(sqlMainId, CommandType.Text).Rows[0]["main_id"].ToString();

            string sql = "select * from vi_cushion_stone  ";
            dt= MySqlHelper.ExecuteDataTable(sql, CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                para = DataTable2Model.FillModel<CushionStoneParameter>(dt.Rows[0]);
                string sqlfindconcrete_no = "select * from sys_dict_item a inner join sys_dict b on a.dict_id=b.id where b.dict_code='concrete_grade'";
                para.concrete_no = FinddictItem(sqlfindconcrete_no, para.concrete_no);

                string sqlfindmain_reinforcement_grade = "select * from sys_dict_item a inner join sys_dict b on a.dict_id=b.id where b.dict_code='steel_grade' ";
                para.reinforcement_grade = FinddictItem(sqlfindmain_reinforcement_grade, para.reinforcement_grade);
                InitCushionStoneSteel();
            }


            string sqlDrawing = "select * from busi_bridge_catalog where  bridge_main_id='" + mainId + "'";
            System.Data.DataTable dtBridgeDrawing = MySqlHelper.ExecuteDataTable(sqlDrawing, CommandType.Text);
            #endregion

            dtProjectTb = dtBridgeDrawing;
        }

        public string FinddictItem(string sql, string value)
        {
            System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sql, CommandType.Text);
            System.Data.DataRow[] dr = dt.Select("item_value='" + value + "'");
            string result = "";
            if (dr.Length > 0)
            {
                result = dr[0]["item_text"].ToString();
            }
            else
            {
                result = value;
            }
            return result;
        }

        public void  InitCushionStoneSteel()
        {
            // 基本尺寸
            Length = para.length;
            Width = para.width;
            Height = para.height;
            CoverThk = para.concrete_protect_layer;

             out_dia= (GeneralRebar.outerDiaOld[para.main_l_reinforcement_diameter]); 
            #region 配筋            

            //ArrAlongW = RebarArrangement.ArrangeRebar(W - CoverThk * 2 - 3 * out_dia, 100);
            //ArrAlongL = RebarArrangement.ArrangeRebar(L - CoverThk * 2 - 3 * out_dia, 100);
            ArrAlongW = RebarArrangement.ArrangeT2BRebarAvg(Width - CoverThk * 2, para.main_w_reinforcement_spacing);
            ArrAlongL = RebarArrangement.ArrangeT2BRebarAvg(Length - CoverThk * 2, para.main_l_reinforcement_spacing);
            int numN1 =ArrAlongW.Count-1;
            int numN2 = (ArrAlongL.Count-1)*(para.reinforcement_net_layers_number-1);
            int numN3 = para.reinforcement_net_layers_number;
            #endregion
            switch (para.reinforcement_grade)
            {
                case "HRB300":
                    strDiameter = "A";
                    break;
                case "HRB335":
                    strDiameter = "B";
                    break;
                case "HRB400":
                    strDiameter = "C";
                    break;
                case "HRB500":
                    strDiameter = "D";
                    break;
                case "HRB500E":
                    strDiameter = "D";
                    break;
                case "HRB600E":
                    strDiameter = "E";
                    break;
                case "HRB650E":
                    strDiameter = "F";
                    break;
                case "HRB700":
                    strDiameter = "G";
                    break;
                default:
                    strDiameter = "D";
                    break;
            }

        }


        public void DrawingPlinth(Database db,int OverAllScale, string bridgeId, out System.Data.DataTable dtProjectConfigTb, string catalogid)
        {
            string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
            System.Data.DataTable  dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
            dtProjectConfigTb = dtProjectConfig;
            System.Data.DataTable dt1; System.Data.DataTable dt2;
            Extents2d ExtPlan, ExtE, ExtDetail;
            Point2d RefPtPlan, RefPtE, RefPtDetail;

            RefPtE = Point2d.Origin;
            ExtE = new Extents2d(RefPtE, RefPtE);
          
            try
            {
                PlotElevation(ref db, out ExtE, RefPtE, OverAllScale);
            }
            catch { }
            RefPtPlan = RefPtE.C2D(ExtE.W() * 1.5, 0);
            ExtPlan = new Extents2d(RefPtPlan, RefPtPlan);
         
            try
            {
                PlotPlan(ref db, out ExtPlan, RefPtPlan.C2D(), OverAllScale);
            }
            catch { }

            RefPtDetail = RefPtPlan.C2D(ExtPlan.W() * 1.5);
            ExtDetail = new Extents2d(RefPtDetail, RefPtDetail);
            try
            {
                CalculateRebar(ref db, ref ExtDetail , RefPtDetail.C2D(), OverAllScale);
            }
            catch { }
            //RendeDetail(ref db, RefPtDetail, out ExtDetail);
            //RendeDetailNew(ref db, RefPtDetail, out ExtDetail, dt1, dt2, OverAllScale);

            LayoutTools.CreateLayout2LRDiv(db, "图纸", ExtE, ExtPlan, ExtDetail,
               Extensions.leftWidth, 5, Extensions.bottomHeight, 10, OverAllScale);
        }

        public void DrawingPlinth(Database db, Point2d pt, int OverAllScale, string bridgeId, out System.Data.DataTable dtProjectConfigTb, string catalogid,ref List<Extents2d> lst)
        {
            string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
            System.Data.DataTable dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
            dtProjectConfigTb = dtProjectConfig;
            System.Data.DataTable dt1; System.Data.DataTable dt2;
            Extents2d ExtPlan, ExtE, ExtDetail;
            Point2d RefPtPlan, RefPtE, RefPtDetail;

            RefPtE = pt;// Point2d.Origin;
            ExtE = new Extents2d(RefPtE, RefPtE);

            try
            {
                PlotElevation(ref db, out ExtE, RefPtE, OverAllScale);
            }
            catch { }
            RefPtPlan = RefPtE.C2D(ExtE.W() * 1.5, 0);
            ExtPlan = new Extents2d(RefPtPlan, RefPtPlan);

            try
            {
                PlotPlan(ref db, out ExtPlan, RefPtPlan.C2D(), OverAllScale);
            }
            catch { }

            RefPtDetail = RefPtPlan.C2D(ExtPlan.W() * 1.5);
            ExtDetail = new Extents2d(RefPtDetail, RefPtDetail);
            try
            {
                CalculateRebar(ref db, ref ExtDetail, RefPtDetail.C2D(), OverAllScale);
            }
            catch { }
            //RendeDetail(ref db, RefPtDetail, out ExtDetail);
            //RendeDetailNew(ref db, RefPtDetail, out ExtDetail, dt1, dt2, OverAllScale);

            //LayoutTools.CreateLayout2LRDiv(db, "图纸", ExtE, ExtPlan, ExtDetail,

            lst.Add(ExtE);
            lst.Add(ExtPlan);
            lst.Add(ExtDetail);
            //   Extensions.leftWidth, 5, Extensions.bottomHeight, 10, OverAllScale);
        }

        // 绘制立面图
        public void PlotElevation(ref Database db, out Extents2d ext, Point2d insertPoint, int scale)
        {
            ext = new Extents2d(insertPoint, insertPoint);
            TextPloter.AddTitle(db,insertPoint, ref ext, "立面", "",  scale);
            Line2d AxisY = new Line2d(insertPoint, insertPoint.C2D(0, 1));
            
            Polyline M1 = new Polyline() { Closed = false };
            Point2d p0 = insertPoint.C2D(-0.5 * Width - 500, -para.ref_height - 15*scale);
            Point2d p1 = p0.C2D(500);
            Point2d p2 = p1.C2D(0, para.ref_height);
            Point2d p22 = p2.Mirror(AxisY);
            Point2d p12 = p1.Mirror(AxisY);
            Point2d p02 = p0.Mirror(AxisY);


            M1.AddVertexAt(0, p0, 0, 0, 0);
            M1.AddVertexAt(1, p1, 0, 0, 0);
            M1.AddVertexAt(2, p2, 0, 0, 0);
            M1.AddVertexAt(3, p22, 0, 0, 0);
            M1.AddVertexAt(4, p12, 0, 0, 0);
            M1.AddVertexAt(5, p02, 0, 0, 0);

            Tools.RenderEntity(db, M1, ref ext, "H细线");
            //ext = ext.Add(((Extents3d)M1.Bounds).C2D());
            #region h标注
            DimPloter.DimRotated(db, ref ext, p22.C3D(), p12.C3D(), p22.C3D(5 * scale), 90, scale,Unit.Millimeter, Unit.Millimeter);
            #endregion

            #region 剖断线
            RebarPLPloter.CreateBreakLine(db, ref ext, p0, p0.C2D(0, -para.anchorage_length -10 * scale));
            RebarPLPloter.CreateBreakLine(db, ref ext, p02, p02.C2D(0, -para.anchorage_length - 10 * scale));
            RebarPLPloter.CreateBreakLine(db, ref ext, p0.C2D(0, -para.anchorage_length - 10 * scale), p02.C2D(0, -para.anchorage_length - 10 * scale));
            #endregion

            #region 钢筋网格
          
            List<Point3d> refRebar;
            RebarPLPloter.CreateGirdElevNew(db, ref ext, p2.C2D(0.5 * Width, -para.concrete_top_protect_layer - 0.5 * out_dia),
                Width - CoverThk * 2, out_dia, ArrAlongW, para.reinforcement_net_layers_number, -para.reinforcement_net_spacing, out refRebar,false);
            Point2d pp0 = p2.C2D(0.5 * Width, -para.concrete_top_protect_layer - 0.5 * out_dia).C2D(-0.5 * (Width - CoverThk * 2) + ArrAlongW[0]*0.5, 0);
            Point2d pp1 = p2.C2D(0.5 * Width, -para.concrete_top_protect_layer - 0.5 * out_dia).C2D(0.5 * (Width - CoverThk * 2) - ArrAlongW[0]*0.5, 0);
            PLPloter.AddLine(db, pp0, pp0.C2D(0, -para.anchorage_length), ref ext, "H钢筋");
            PLPloter.AddLine(db, pp1, pp1.C2D(0, -para.anchorage_length), ref ext, "H钢筋");
            #endregion

            #region  箭头引线
            double width = 0,height= para.reinforcement_net_spacing * (para.reinforcement_net_layers_number-1);
            width = ArrAlongW.Sum();
            List<double> lList = new List<double>(); 
            for(int i=0;i< para.reinforcement_net_layers_number-1; i++)
            {
                lList.Add(para.reinforcement_net_spacing);
            }
            List<double> wList=new List<double>();
            for(int i=1;i< ArrAlongW.Count;i++)
            {
                  wList.Add(ArrAlongW[i]);
            }
            //3
            //RebarPLPloter.CreateLineArrowDB(db, ref ext, p2.C2D(0.5 * Length, -CoverThk - 0.5 * out_dia).C3D(-width / 3, -100), ArrowDirection.North, lList, wList, "2", scale);
            wList = new List<double>();

            Point3d p00 = p2.C2D(0.5 * Width, -para.concrete_top_protect_layer - 0.5 * out_dia).C3D(0, 0).C3D(0, 0);
            //1
            RebarPLPloter.CreateLineArrowDB(db, ref ext, p00.C3D(0.5 * Width-CoverThk-0.5* ArrAlongW[0] * 0.5-0.5*out_dia, -0.5*para.anchorage_length), ArrowDirection.West, new List<double>(), new List<double>(), "1", scale,true,3);
            #endregion

            refRebar.Add(p2.C3D());
            refRebar.Add(p22.C3D());
            refRebar.Sort((x, y) => x.X.CompareTo(y.X));

            #region 上部和左侧标注
            //上部标注
            DimPloter.AddDoubleListDimRotated(db, ref ext, p2.C3D(0, 5 * scale), refRebar, scale,0, para.main_w_reinforcement_spacing);

            //左侧标注
            List<Point3d> refLength0=new List<Point3d>();
            refLength0.Add(p2.C3D());
            refLength0.Add(p00.C3D(0,-out_dia));   
            List<Point3d> refLength = new List<Point3d>();
            refLength.Add(p00.C3D(0, -out_dia));      
            refLength.Add(p00.C3D(0, -out_dia - height));

            DimPloter.AddListDimRotated(db, ref ext, refLength0[0].C3D(-5 * scale - CoverThk),
         refLength0, scale, 90);
            DimPloter.AddListDimRotated(db, ref ext, refLength0[0].C3D(-5 * scale - CoverThk),
                 refLength, scale, 90, para.reinforcement_net_spacing);
            #endregion

            //#region 标记B 
            //RebarPLPloter.AddBJByDirectiong(db, ref ext, p0.C3D(500, 480), ArrowDirection.North, "B", scale);
            //RebarPLPloter.AddBJByDirectiong(db, ref ext, p0.C3D(500, -570), ArrowDirection.South, "B", scale);
            //#endregion

            #region  多引线圆标记
            List<Point3d> N1List = new List<Point3d>();
            for(int i=2;i< refRebar.Count-2;i++)
            {
                if(i<4||i>= refRebar.Count - 4)
                    N1List.Add(refRebar[i]);
            }
            RebarPLPloter.CreateMultiLineCicle(db, ref ext, p0.C3D(500+Width/2,height+ 2.5*scale), N1List, "2", scale);

            List<Point3d> N2List = new List<Point3d>();
            for (int n = 1; n < para.reinforcement_net_layers_number; n++)
            {
                for (int i = 2; i < refRebar.Count - 2; i++)
                {
                    if (i < 4 || i >= refRebar.Count - 4)
                        N2List.Add(refRebar[i].C3D(0, -para.main_w_reinforcement_spacing+(n-2)* para.reinforcement_net_spacing));
                }
            }
            RebarPLPloter.CreateMultiLineCicle(db, ref ext, p0.C3D(500 + Width / 2, - 0.5 * out_dia-15*scale), N2List,"2", scale);

            List<Point3d> ptLLst = new List<Point3d>();
            for (int n = 0; n < para.reinforcement_net_layers_number; n++)
            {
                ptLLst.Add(refRebar[1].C3D(0, (n - 2) * para.reinforcement_net_spacing));
            }
            SteelBarPileCap.CreateRebarPointLine(db, ref ext, eDirection.E_LEFT, ptLLst, "3", 1, 0, scale, false, 5 * scale);

            List<Point3d> ptRLst = new List<Point3d>();
            for (int n = 0; n < para.reinforcement_net_layers_number; n++)
            {
                ptRLst.Add(refRebar[refRebar.Count-2].C3D(0, (n - 2) * para.reinforcement_net_spacing));
            }
            SteelBarPileCap.CreateRebarPointLine(db, ref ext, eDirection.E_RIGHT, ptRLst, "3", 1, 0, scale, false, 5 * scale);
            #endregion

            #region  1号钢筋长度

            //Point2d pinsert = p2.C2D(0.5 * Width, -CoverThk - 0.5 * out_dia);
            //PLPloter.AddLine(db, pinsert.C2D(-curRebar.LineSegmentList[1] / 2, 0), pinsert.C2D(-curRebar.LineSegmentList[1] / 2, -height-5*scale), ref ext,"H细线");
            //PLPloter.AddLine(db, pinsert.C2D(curRebar.LineSegmentList[1] / 2, 0), pinsert.C2D(curRebar.LineSegmentList[1] / 2, -height - 5 * scale) ,ref ext, "H细线");
            #endregion
        }



        /// <summary>
        /// 绘制平面图
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="insertPoint"></param>
        /// <param name="scale"></param>
        public void PlotPlan(ref Database db, out Extents2d ext, Point2d insertPoint, int scale)
        {
            Point2d CT = insertPoint;


            ext = new Extents2d(CT, CT);

            TextPloter.AddTitle(db, CT, ref ext, "平面", "", scale);

            Point2d M1 = CT.C2D(0, -15 * scale - CoverThk);

            PLPloter.AddPloy4(db, M1, 0.5 * Width, 0.5 * Width, Length, ref ext, "H细线");

            #region 网格
            List<Point3d> leftPts, topPts;
            RebarPLPloter.CreateLineGrid(db, M1.C2D(-0.5 * Width + CoverThk, -CoverThk), ArrAlongL, ArrAlongW,
                ref ext, 0, out leftPts, out topPts);
            #endregion

            #region 箭头引线
            double width = 0;
            double height = 0;
            width = ArrAlongW.Sum();
            height = ArrAlongL.Sum();
            if (para.is_exist_stirrup == "0")
            {
                RebarPLPloter.CreateLineArrowDB(db, ref ext, topPts[0].C3D(0, -height / 2), ArrowDirection.West, ArrAlongL, ArrAlongW, "2", scale);
                RebarPLPloter.CreateLineArrowDB(db, ref ext, leftPts[leftPts.Count - 1].C3D(width / 2, 0), ArrowDirection.South, ArrAlongL, ArrAlongW, "1", scale);
            }
            else
            {
                List<double> N1List = ArrAlongL;
                N1List.RemoveAt(N1List.Count - 1);
                N1List.RemoveAt(0);
                List<double> N2List = ArrAlongW;
                N2List.RemoveAt(N2List.Count - 1);
                N2List.RemoveAt(0);
                RebarPLPloter.CreateLineArrowDB(db, ref ext, topPts[1].C3D(0, -height / 2), ArrowDirection.West, N1List, N2List, "2", scale, true, 5);
                RebarPLPloter.CreateLineArrowDB(db, ref ext, leftPts[leftPts.Count - 2].C3D(width / 2, 0), ArrowDirection.South, N1List, N2List, "1", scale, true, 2 );

                RebarPLPloter.CreateLineArrowDB(db, ref ext, topPts[topPts.Count - 1].C3D(0, -height / 3), ArrowDirection.West, new List<double>() { N1List[0] }, new List<double>(), "3", scale, true, 8 );
            }
            #endregion

            #region 标注
            // 两层左侧标注
            DimPloter.AddListDimRotated(db, ref ext, leftPts[0].C3D(-5 * scale - CoverThk),
                new List<Point3d>() { M1.C3D(0, -Length), leftPts[0], leftPts[1] }, scale, 90);
            DimPloter.AddListDimRotated(db, ref ext, leftPts[0].C3D(-5 * scale - CoverThk),
                 new List<Point3d>() { leftPts[1], leftPts[leftPts.Count - 2] }, scale, 90, para.main_l_reinforcement_spacing);
            DimPloter.AddListDimRotated(db, ref ext, leftPts[0].C3D(-5 * scale - CoverThk),
                new List<Point3d>() { leftPts[leftPts.Count - 2], leftPts[leftPts.Count - 1], M1.C3D() }, scale, 90);
            DimPloter.AddListDimRotated(db, ref ext, leftPts[0].C3D(-5 * scale - CoverThk - 5 * scale),
                new List<Point3d>() { M1.C3D(0, -Length), M1.C3D() }, scale, 90);

            // 上部标注
            //DimPloter.AddListDimRotated(db, ref ext, topPts[0].C3D(0, 5 * scale + CoverThk), topPts, 20);
            //DimPloter.AddListDimRotated(db, ref ext, topPts[0].C3D(0, 5 * scale + CoverThk + 5 * scale),
            //    new List<Point3d>() { M1.C3D(-Width * 0.5), M1.C3D(Width * 0.5) }, 20);
            //;
            topPts.Add(M1.C3D(-Width * 0.5).C3D());
            topPts.Add(M1.C3D(Width * 0.5).C3D());
            topPts.Sort((x, y) => x.X.CompareTo(y.X));
            DimPloter.AddDoubleListDimRotated(db, ref ext, topPts[0].C3D(0, 5 * scale + CoverThk), topPts, scale, 0, para.main_w_reinforcement_spacing);
            #endregion
        }


        /// <summary>
        /// 绘制钢筋
        /// </summary>
        /// <param name="db"></param>
        /// <param name="insertPoint"></param>
        /// <param name="ext"></param>
        public void RendeDetailNew(ref Database db, Point2d insertPoint, ref Extents2d ext,
             double scale = 50)
        {
            //ext = new Extents2d(insertPoint, insertPoint);
            Point2d CT = insertPoint;
            Point2d M1 = CT.C2D(0, -2 * scale);

            //PolylinePloter.AddPloy4(db, ref ext, M1, 0.5 * 10, 0.5 * 10, 20);

            // 查询数据
            //string selectString = string.Format("SELECT * FROM span_tbl where Name='{0}'", SpName);
            //var consetr = string.Format("data source=cdb-2ashfo5g.bj.tencentcdb.com;port=10033;user id=wyz;password=wang1234;DataBase=nep2021;charset=utf8"); ;// string.Format("server={0};port={3};user id={1};password={2};charset=utf8;database={4}", address, user, password, port,database);
            //System.Data.DataTable dt = SRBMain.MySqlHelper.(consetr, selectString, System.Data.CommandType.Text);

            // 绘制表格
            List<string> list = new List<string>();
            list.Add("编号");
            list.Add("直径\\P(mm)");
            list.Add("单根长\\P(mm)");
            list.Add("根数");
            list.Add("总长\\P(m)");
            list.Add("单位重\\P(kg/m)");
            list.Add("总重\\P(kg)");
            //list.Add("小计(kg)");
            double height = 0;
            double width = 0;
            PierModelTablePloter.DrawCushionRebarTable(db, ref ext, M1.C3D(-0.5 * Width + CoverThk), rebarLst, list, rebarLst.Count + 1, list.Count, ref height, ref width, Extensions.curFont, scale, para.reinforcement_grade);
            double ActualWidth = 0, ActualHeight = 0;
            TextPloter.GetTextActualWidth(db, "单个垫石钢筋明细表", scale, out ActualWidth, out ActualHeight, 5);
            TextPloter.AddMText(db,M1.C3D(-0.5 * Width + CoverThk + (width - ActualWidth) / 2, ActualHeight + scale * 2), ref ext, "单个垫石钢筋明细表", scale, 5, ActualWidth + 5, Extensions.curFont);

            List<string> list1 = new List<string>();
            list1.Add("名称及规格");
            list1.Add("名称及规格");
            list1.Add("单位");
            list1.Add("总重");
            list1.Add("合计");
            double height1 = 0;
            double width1 = 0;
            TextPloter.GetTextActualWidth(db, "垫石材料数量表(全桥" + 2 + "个)", scale, out ActualWidth, out ActualHeight, 5);

            double volumn = para.width * 0.001 * para.length * 0.001 * para.height * 0.001;
            PierModelTablePloter.DrawCushionMaterialsTable(db, ref ext, M1.C3D(-0.5 * Width + CoverThk, -height - ActualHeight * 3), rebarLst, list1, 2, volumn, list1.Count, ref height1, ref width1, Extensions.curFont, scale,para.reinforcement_grade,para.concrete_no);
            //TextPloter.AddMText(db, M1.C3D(-0.5 * Width + CoverThk + (width1 - ActualWidth) / 2, -height - ActualHeight * 2 + scale * 2), ref ext, "垫石材料数量表(全桥" + plinthParameter.structureNumber + "个)", scale, 5,80, Extensions.curFont);
            TextPloter.AddMText(db,M1.C3D(-0.5 * Width + CoverThk + (width1 - ActualWidth) / 2, -height - ActualHeight * 2 + scale * 2), ref ext, "垫石材料数量表(全桥" +2 + "个)", scale, 5, ActualWidth + 2 * scale, Extensions.curFont);

            //// 文字
            //string text = "注";
            //Point3d p1 = M1.C3D(-0.5 * Width + CoverThk + width1 + scale * 5, -height - ActualHeight * 3);
            //TextPloter.AddMText(db, p1, ref ext, text, scale, 2.5,10, Extensions.curFont);

            string text = @"注
1.本图尺寸均以毫米为单位。";
            Point3d p2 = M1.C3D(-0.5 * Width + CoverThk + width1 + scale * 5, -height - ActualHeight * 4);
            //TextPloter.AddMText(db, p2, ref ext, text, scale, 2.5,44, Extensions.curFont);
            TextPloter.AddMText(db,p2, ref ext, text, scale, 2.5, 50 * scale, Extensions.curFont);

        }

        public void CalculateRebar(ref Database db, ref Extents2d ext, Point2d insertPoint, double scale)
        {
            int numN1 = ArrAlongW.Count + 1;
            int numN2 = (ArrAlongL.Count + 1) * (para.reinforcement_net_layers_number - 1);
            int numN3 = para.reinforcement_net_layers_number;
            Tuple<int, eSampleType, eDB, int, double, string> tu;
            double N1Dia = (GeneralRebar.outerDiaOld[para.main_w_reinforcement_diameter]);
            double hoke = GeneralRebar.outerDiaHoke[para.main_w_reinforcement_diameter] ;
            double hokeR = GlobalFuncs.CalculateReduction(para.main_w_reinforcement_diameter, 90);
            double lenN1 =(numN1-1) * para.main_w_reinforcement_spacing + N1Dia*2;
            double lenTotalN1 = para.anchorage_length * 2 + lenN1 - hoke*2- hokeR*2;
            double lenN2 = para.length -CoverThk*2;

            double hoke3 = GeneralRebar.outerDiaHoke[para.stirrup_reinforcement_diameter] ;
            double hoke31 = GeneralRebar.outerDia135Hoke[para.stirrup_reinforcement_diameter];
            double hoke31R =GlobalFuncs.CalculateReduction(para.stirrup_reinforcement_diameter, 90);
          
            double len3 = para.length - CoverThk * 2;
            double len3w = para.width - CoverThk * 2;
            double lenTotalN3 = len3 * 2 + len3w * 2 + hoke31 * 4;
            tu = new Tuple<int, eSampleType, eDB, int, double, string>(1, eSampleType.UT, para.main_w_reinforcement_diameter, numN1, lenTotalN1, strDiameter);
            rebarLst.Add(tu);
            Point2d pt1 = insertPoint.C2D();
            SteelBarPileCap.AddLineSample(pt1.C2D(-lenN1*0.5), pt1.C2D(lenN1 * 0.5), ref ext, "H钢筋", eSampleType.UB, para.anchorage_length*0.5);

            string valueS = (lenTotalN1).ToString("0");
            string noS = numN1 + strDiameter + ((int)para.main_w_reinforcement_diameter).ToString();
            RebarDimPloter.RebarWireNew(db, ref ext, pt1.C2D(lenN1 * 0.1), noS, valueS, "1",
               GeTools.DegreeToRadian(0), Extensions.curFont, scale);
            TextPloter.AddDBText(db, pt1.C3D(-lenN1 * 0.25, 1 * scale), ref ext, lenN1.ToString(), scale, 2.5, "SRB.HT", 0);
            TextPloter.AddDBText(db, pt1.C3D(-lenN1 * 0.5, -para.anchorage_length*0.25), ref ext, para.anchorage_length.ToString(), scale, 2.5, "SRB.HT", GeTools.DegreeToRadian(90));
            TextPloter.AddDBText(db, pt1.C3D(lenN1 * 0.5+4*scale, -para.anchorage_length * 0.25), ref ext, para.anchorage_length.ToString(), scale, 2.5, "SRB.HT", GeTools.DegreeToRadian(90))
                ;
            tu = new Tuple<int, eSampleType, eDB, int, double, string>(2, eSampleType.UT, para.main_l_reinforcement_diameter, numN2, lenN2, strDiameter);
            rebarLst.Add(tu);

            Point2d pt2 = insertPoint.C2D(0,-ext.L()*1.1);
            SteelBarPileCap.AddLineSample(pt2.C2D(-lenN2 * 0.5), pt2.C2D(lenN2 * 0.5), ref ext, "H钢筋", eSampleType.UN);
            valueS = (lenN2).ToString("0");
            noS = numN2 + strDiameter + ((int)para.main_l_reinforcement_diameter).ToString();
            RebarDimPloter.RebarWireNew(db, ref ext, pt2.C2D(), noS, valueS, "2",
               GeTools.DegreeToRadian(0), Extensions.curFont, scale);

            tu = new Tuple<int, eSampleType, eDB, int, double, string>(3, eSampleType.UT, para.stirrup_reinforcement_diameter, numN3, lenTotalN3, strDiameter);
            rebarLst.Add(tu);

            Point2d pt3 = insertPoint.C2D(0, -ext.L() * 1.1);
            PLPloter.AddPloy4(db, pt3, len3w * 0.5, len3w * 0.5, len3, ref ext, "H钢筋");
            //PLPloter.AddLine(db,ptCenter.C2D(N810w * 0.5), ptCenter.C2D(N810w * 0.5).C2D(-1.5*scale, -2 * scale), ref ext, "H钢筋");
            //PLPloter.AddLine(db,ptCenter.C2D(N810w * 0.5), ptCenter.C2D(N810w * 0.5).C2D(-2 * scale, -1.5 * scale), ref ext, "H钢筋");
            PLPloter.AddLine(db, pt3.C2D(len3w * 0.5 - 3,-len3), pt3.C2D(len3w * 0.5 - 3, -len3).C2D(-1.5 * scale, 1.5 * scale), ref ext, "H钢筋");
            PLPloter.AddLine(db, pt3.C2D(len3w * 0.5, 3 -len3), pt3.C2D(len3w * 0.5, 3 -len3).C2D(-1.5 * scale, 1.5 * scale), ref ext, "H钢筋");
            valueS = (lenTotalN3).ToString("0");
            noS = numN3+strDiameter + ((int)para.stirrup_reinforcement_diameter).ToString();
            RebarDimPloter.RebarWireNew(db, ref ext, pt3.C2D(lenN1 * 0.15), noS, valueS, "3",
            GeTools.DegreeToRadian(0), Extensions.curFont, scale);
            TextPloter.AddDBText(db, pt3.C3D(-lenN1 * 0.25, 1*scale), ref ext, len3w.ToString("0"), scale, 2.5, "SRB.HT", 0);
            TextPloter.AddDBText(db, pt3.C3D(-lenN1 * 0.5-3*scale, -len3 * 0.5), ref ext, len3.ToString("0"), scale, 2.5, "SRB.HT", GeTools.DegreeToRadian(90));

            rebarLst.Sort();

            RendeDetailNew(ref db, insertPoint.C2D(0, -ext.L() * 1.1), ref ext, scale);

        }

        private void BatchInsertQuentity(List<Tuple<int, eSampleType, eDB, int, double, string>> rebarLst, List<Tuple<eDB, double, double, string>> tuNet)
        {
            List<string> sqlList = new List<string>();
            if (rebarLst.Count > 0)
            {
                eDB mainWDia = para.main_w_reinforcement_diameter;
                eDB mainDia = para.main_l_reinforcement_diameter;
                string main_reinforcement_grade = para.reinforcement_grade;
                string reinforcement_grade = para.reinforcement_grade;
                string delSql = "delete from busi_engineering_quantity where  bridge_id='" + bridgeId + "' and (portable_belong_part=7 or belong_part=7) ";
                #region 数据合并
                // 数据合并
                Dictionary<string, List<int>> dic = new Dictionary<string, List<int>>();
                for (int i = 0; i < rebarLst.Count; ++i)
                {
                    string strNo = rebarLst[i].Item3.ToString();
                    if (dic.Count == 0)
                    {
                        List<int> list = new List<int>();
                        list.Add(i);
                        dic.Add(strNo, list);
                    }
                    else
                    {
                        if (!dic.ContainsKey(strNo))
                        {
                            List<int> list = new List<int>();
                            list.Add(i);
                            dic.Add(strNo, list);
                        }
                        else
                        {
                            List<int> list = new List<int>();
                            dic.TryGetValue(strNo, out list);
                            list.Add(i);
                            dic[strNo] = list;
                        }
                    }
                }
                //dic = dic.OrderByDescending(f => f.Key).ToDictionary(f => f.Key, f => f.Value);
                #endregion
                dic = dic.OrderByDescending(f => f.Key).ToDictionary(f => f.Key, f => f.Value);
                string remark = "垫石钢筋";
                string material = "钢筋";
                string materialCode = main_reinforcement_grade;
                string materielSpecs = mainDia.ToString();


                double allWeight = 0;
                int count =2;
                int totalCap = 0;
                int totalWCap = 0;
                //int netCount = tuNet.Count;
                foreach (KeyValuePair<string, List<int>> pr in dic)
                {
                    string strNo = "钢筋";
                    string unit = "kg";
                    eDB diameter = eDB.None;
                    double totallength = 0;
                    // 设置表数据
                    for (int i = 0; i < pr.Value.Count; ++i)
                    {
                        // 钢筋编号
                        diameter = rebarLst[pr.Value[i]].Item3;                         // 直径
                        totallength += rebarLst[pr.Value[i]].Item4 * rebarLst[pr.Value[i]].Item5;
                        if (diameter == mainDia)
                        {
                            if (rebarLst[pr.Value[i]].Item5 * 0.01 > 12)
                            {
                                totalCap += ((int)(rebarLst[pr.Value[i]].Item5 * 0.01 / 12)) * rebarLst[pr.Value[i]].Item4;
                            }
                        }
                        else if (diameter == mainWDia)
                        {
                            if (rebarLst[pr.Value[i]].Item5 * 0.01 > 12)
                            {
                                totalWCap += ((int)(rebarLst[pr.Value[i]].Item5 * 0.01 / 12)) * rebarLst[pr.Value[i]].Item4;
                            }
                        }
                    }
                    double weight = GlobalFuncs.GetUnitWeight(diameter);
                    double totalWeight = totallength * 0.01 * weight;

                    allWeight += totalWeight * count;
                    string strDiameter = "Ф" + (int)diameter;

                    remark = "垫石钢筋";
                    material = "钢筋";
                    materialCode = main_reinforcement_grade;
                    materielSpecs = diameter.ToString();
                    if (diameter == mainDia || diameter == mainWDia)
                    {
                        materialCode = main_reinforcement_grade;

                    }
                    else
                        materialCode = reinforcement_grade;
                    string id = Guid.NewGuid().ToString();
                    string sql = "insert into busi_engineering_quantity(id,bridge_id,materiel,materiel_code,materiel_specs,unit,"
                        + "unit_weight,total_weight,is_auto,belong_part,portable_belong_part,remark)"
                        + "values('" + id + "','" + bridgeId + "','" + material + "','" + materialCode + "','" + materielSpecs + "','kg','"
                        + weight + "','" + allWeight + "','2','7','7','" + remark + "')";
                    sqlList.Add(sql);
                }

               
                double volumn = para.width * 0.001 * para.length * 0.001 * para.height * 0.001;
                material = "混凝土";
                materialCode = material;
                materielSpecs = para.concrete_no;
                remark = "承台混凝土";
                string id1 = Guid.NewGuid().ToString();
                string sqlIn = "insert into busi_engineering_quantity(id,bridge_id,materiel,materiel_code,materiel_specs,unit,"
                    + "total_weight,is_auto,belong_part,portable_belong_part,remark)"
                    + "values('" + id1 + "','" + bridgeId + "','" + material + "','" + materialCode + "','" + materielSpecs + "','m3','"
                    + volumn * count + "','2','7','7','" + remark + "')";
                sqlList.Add(sqlIn);
              
            }

        }

    }
}
